<template>
  <div class="home-singer">
    <h1>热门歌手</h1>
    <div class="wrapper" ref="wrapper">
      <!--滚动所需的容器-->
      <ul class="content" ref="content">
        <li
          v-for="(item, index) in listData"
          :key="index"
          @click="selectItem(item)"
        >
          <div class="pic">
            <van-image :src="item.singer_pic" radius="10px" />
          </div>
          <span>{{ item.singer_name }}</span>
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
import { betterScrollHorizontal } from "../../common/betterScroll.js"; //引入betterscroll

export default {
  data() {
    return {
      listData: [],
    };
  },
  created() {
    this.getSingerListData();
  },
  methods: {
    getSingerListData() {
      this.$request("get", "/singer/list").then((res) => {
        console.log(res);
        if (res.result == 100) {
          this.listData = res.data.list.slice(0, 80);
        }
        // 异步数据的处理
        betterScrollHorizontal(
          this,
          this.$refs.wrapper,
          this.$refs.content,
          this.listData.length,
          10
        ); //调用横向滚动的方法
      });
    },
    selectItem(item) {
      const id = item.singer_mid;
      // 调用状态管理，commit提交唤醒mutations
      this.$store.commit('saveSinger',item)
      this.$router.push({
        path: `/singerhome/${id}`,
      });
    },
  },
};
</script>
<style scoped>
.wrapper {
  width: 100%; /** 容器的宽度就是屏幕的宽度 */
  overflow: hidden;
}

ul.content li {
  /* li默认使用块级元素 */
  display: inline-block; /**行内元素*/
  width: 10rem;
  vertical-align: top; /**垂直方向的algin顶满上一个div */
}
ul.content li * {
  /**选取下面的所有元素*/
  margin: 0.1rem;
}
ul.content li > span {
  font-size: 0.8rem;
  color: rgb(41, 31, 24);
}
.home-singer > h1 {
  text-align: center;
  font-size: 22px;
}
</style>